package com.fortune.app.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.cardview.widget.CardView;

import com.fortune.app.R;
import com.fortune.app.activities.ZhouYiActivity;
import com.fortune.app.activities.TarotActivity;
import com.fortune.app.activities.ZodiacActivity;
import com.fortune.app.activities.DirectionActivity;
import com.fortune.app.FortuneApplication;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 首页Fragment，显示每日运势概览和功能入口
 */
public class HomeFragment extends Fragment {
    private TextView tvDate;
    private TextView tvUserName;
    private TextView tvDailyFortune;
    private TextView tvLuckyDirection;
    private CardView cardZhouYi;
    private CardView cardTarot;
    private CardView cardZodiac;
    private CardView cardDirection;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        
        // 初始化视图
        tvDate = view.findViewById(R.id.tv_date);
        tvUserName = view.findViewById(R.id.tv_user_name);
        tvDailyFortune = view.findViewById(R.id.tv_daily_fortune);
        tvLuckyDirection = view.findViewById(R.id.tv_lucky_direction);
        cardZhouYi = view.findViewById(R.id.card_zhouyi);
        cardTarot = view.findViewById(R.id.card_tarot);
        cardZodiac = view.findViewById(R.id.card_zodiac);
        cardDirection = view.findViewById(R.id.card_direction);
        
        // 设置当前日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE", Locale.CHINA);
        tvDate.setText(dateFormat.format(new Date()));
        
        // 设置用户信息
        FortuneApplication app = FortuneApplication.getInstance();
        if (app.isUserInfoSet()) {
            tvUserName.setText("尊敬的用户，");
        } else {
            tvUserName.setText("尊敬的访客，");
        }
        
        // 设置每日运势（示例）
        tvDailyFortune.setText("今日运势：★★★★☆ 整体运势良好，适合开展新项目和社交活动。");
        
        // 设置吉凶方位（示例）
        tvLuckyDirection.setText("今日吉方：东南 凶方：西北");
        
        // 设置卡片点击事件
        setupCardClickListeners();
        
        return view;
    }
    
    /**
     * 设置功能卡片的点击事件
     */
    private void setupCardClickListeners() {
        cardZhouYi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ZhouYiActivity.class));
            }
        });
        
        cardTarot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), TarotActivity.class));
            }
        });
        
        cardZodiac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ZodiacActivity.class));
            }
        });
        
        cardDirection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), DirectionActivity.class));
            }
        });
    }
    
    /**
     * 更新每日运势信息
     */
    public void updateDailyFortune() {
        // 实际应用中，这里应该从数据库或缓存中获取最新的运势信息
        // 这里仅作为示例
        FortuneApplication app = FortuneApplication.getInstance();
        if (app.isUserInfoSet()) {
            String birthDate = app.getUserBirthDate();
            // 使用星座运势模块生成今日运势
            // ZodiacFortune zodiacFortune = new ZodiacFortune();
            // Map<String, Object> result = zodiacFortune.generateDailyHoroscope(birthDate, null);
            // 更新UI
        }
    }
}
